package model;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Database_Model {

    private boolean ldapEnabled;
    private String serverName;
    private Connection connection;
    private static String databaseName = "cp3120d4";
    private static String databaseUsername = "root";
    private static String databasePassword = "root";
    private static String driver = "com.mysql.jdbc.Driver";

    public Connection getConnection() {
        createConnection();
        return connection;
    }

    public void createConnection() {
        if (connection == null) {
            try {
                Class.forName(driver);
                String url = "jdbc:mysql://127.0.0.1:3306/" + databaseName;

                connection = DriverManager.getConnection(
                        url, databaseUsername, databasePassword);

            } catch (Exception e) {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Database connection could not be established.  Please" +
                        "check your network connection and restart.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
                connection = null;
            }
        }
    }

    public boolean ldapEnabled() {
        return ldapEnabled;
    }

    public void setLdapEnabled(boolean ldapEnabled) {
        this.ldapEnabled = ldapEnabled;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }
}
